package com.module.compose.base.composable

import android.app.Activity
import androidx.annotation.DrawableRes
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Card
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.material.IconButton
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.module.compose.base.R
import com.module.compose.base.utils.*
import com.module.compose.base.viewModel.PageState

/**
 * 无限循环进度条展示，居中
 */
@Composable
fun ProcessView() {
    Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Center) {
        CircularProgressIndicator(
            Modifier.size(25.dp),
            color = RandomColor.color(),
            strokeWidth = 2.dp
        )
    }
}

/**
 * 标题栏
 */
@Composable
fun TitleBar(title: String, isShowBack: Boolean = true) {
    val context = LocalContext.current as Activity
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .height(48.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        if (isShowBack) {
            IconButton(onClick = {
                context.finish()
            }) {
                Image(
                    painter = painterResource(id = R.mipmap.icon_back),
                    contentDescription = null,
                    modifier = Modifier
                        .width(45.dp)
                        .height(45.dp)
                        .padding(13.dp)
                )
            }
        }
        Text(
            text = title,
            fontSize = 16.sp,
            fontWeight = FontWeight.Bold,
            color = TextBlack,
            modifier = if (isShowBack) {
                Modifier.padding(start = 0.dp)
            } else {
                Modifier.padding(start = 15.dp)
            }
        )
    }
}

/**
 * 通用的页面
 */
@Composable
fun CommonPage(
    title: String,
    isShowBack: Boolean = true,
    pageState: PageState = PageState.SUC,
    content: @Composable ColumnScope.() -> Unit
) {
    Column(Modifier.fillMaxSize()) {
        TitleBar(title = title, isShowBack = isShowBack)
        when (pageState) {
            PageState.SUC -> Column(Modifier.fillMaxSize(), content = content)
            PageState.LOADING -> ProcessView()
            PageState.EMPTY -> Column(
                Modifier.fillMaxSize(),
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center
            ) {
                Text(text = "无数据")
            }
            PageState.ERR -> Column(
                Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center
            ) {
                Text(text = "数据错误")
            }
        }

    }
}

/**
 * 通用CardView
 */
@Composable
fun CommonCard(
    modifier: Modifier = Modifier,
    elevation: Dp = 0.dp,
    shape: Shape = RoundedCornerShape(8.dp),
    backgroundColor: Color = White,
    content: @Composable () -> Unit
) {
    Card(
        elevation = elevation,
        shape = shape,
        backgroundColor = backgroundColor,
        modifier = modifier,
        content = content
    )
}

/**
 * 通用的listView的Item
 */
@Composable
fun CommonItem(
    content: String,
    @DrawableRes iconId: Int,
    showArrow: Boolean = true,
    onItemClick: () -> Unit = {}
) {
    //卡片控件
    CommonCard(
        Modifier
            .padding(start = 15.dp, end = 15.dp, top = 10.dp, bottom = 10.dp)
            .fillMaxWidth()
    ) {
        Row(
            Modifier
                .fillMaxWidth()
                .clickable { onItemClick() }
                .padding(start = 15.dp, end = 15.dp),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.SpaceBetween
        ) {
            Row(
                verticalAlignment = Alignment.CenterVertically,
            ) {
                Image(
                    painter = painterResource(iconId),
                    contentDescription = null,
                    Modifier
                        .width(20.dp)
                        .height(20.dp)
                )
                Text(
                    text = content,
                    Modifier
                        .padding(20.dp),
                    fontSize = 16.sp,
                    color = TextBlack
                )
            }
            if (showArrow) {
                Image(
                    painter = painterResource(id = R.mipmap.icon_arrow_go),
                    contentDescription = null,
                    Modifier
                        .width(15.dp)
                        .height(15.dp)
                )
            }
        }
    }
}

/**
 * 用于展示代码的控件
 */
@Composable
fun CodeView(code: String) {
    CommonCard(
        Modifier
            .fillMaxWidth()
            .padding(top = 15.dp, bottom = 15.dp), backgroundColor = CodeBg
    ) {
        LazyRow {
            item {
                Text(
                    modifier = Modifier.padding(15.dp),
                    text = code,
                    fontSize = 16.sp,
                    fontWeight = FontWeight.SemiBold,
                    color = TextLightGray,
                    lineHeight = 30.sp
                )
            }
        }

    }
}

/**
 * 用于展示内容文字的容器
 */
@Composable
fun ContentView(content: @Composable ColumnScope.() -> Unit) {
    CommonCard(
        Modifier
            .fillMaxWidth()
            .padding(top = 15.dp, bottom = 15.dp), backgroundColor = White
    ) {
        Column(content = content)
    }
}

/**
 * 内容控件
 */
@Composable
fun Content(
    content: String,
    isNeedScroll: Boolean = true,
    fontStyle: FontStyle = FontStyle.Normal,
    color: Color = TextDark,
    onClick: () -> Unit = {},
) {
    if (isNeedScroll) {
        LazyRow {
            item {
                Text(
                    modifier = Modifier
                        .padding(15.dp)
                        .clickable {
                            onClick()
                        },
                    text = content,
                    fontSize = 16.sp,
                    fontWeight = FontWeight.SemiBold,
                    color = color,
                    lineHeight = 30.sp,
                    fontStyle = fontStyle,
                )
            }
        }
    } else {
        Text(
            modifier = Modifier.padding(15.dp),
            text = content,
            fontSize = 16.sp,
            fontWeight = FontWeight.SemiBold,
            color = TextDark,
            lineHeight = 30.sp
        )
    }
}

/**
 * 标题
 */
@Composable
fun Title(title: String, color: Color = TextBlack, lineHeight: TextUnit = 30.sp) {
    Text(
        modifier = Modifier.padding(15.dp),
        text = title,
        fontSize = 18.sp,
        fontWeight = FontWeight.Bold,
        color = color,
        lineHeight = lineHeight
    )
}